<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="档案名称" prop="archiveName">
        <el-input
          v-model="queryParams.archiveName"
          placeholder="请输入档案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="档案类型" prop="archiveTypeId">
        <el-select v-model="queryParams.archiveTypeId" placeholder="请选择档案类型" clearable>
          <el-option
            v-for="dict in dict.type.archive_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:DArchive:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"

          @click="handleUpdate"
          v-hasPermi="['system:DArchive:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:DArchive:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:DArchive:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DArchiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="档案编号" align="center" prop="id" />
      <el-table-column label="档案名称" align="center" prop="archiveName" />
      <el-table-column label="档案类型" align="center" prop="archiveType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.archive_type" :value="scope.row.archiveTypeId"/>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" align="center" prop="enterpriseName" />
      <!-- <el-table-column label="企业编号" align="center" prop="enterpriseId" /> -->
      <el-table-column label="部门名称" align="center" prop="departmentName" />
      <!-- <el-table-column label="部门编号" align="center" prop="departmentId" /> -->
      <el-table-column label="提交人名称" align="center" prop="userName" />
      <!-- <el-table-column label="提交人编号" align="center" prop="userId" /> -->
      <el-table-column label="档案内容简述" align="center" prop="archiveContent" />
      <!-- <el-table-column label="档案文件地址" align="center" prop="archiveAddress" /> -->
      <el-table-column label="所属案卷名" align="center" prop="docName" />
      <el-table-column label="所属案卷编号" align="center" prop="docNumber" />
      <el-table-column label="保管期限" align="center" prop="saveTime" />
      <el-table-column label="是否可借阅" align="center" prop="secrecyLevel" >
        <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.secrecyLevel == '机密' ">不可借阅</el-tag>
            <el-tag v-if="scope.row.secrecyLevel == '解密' ">可借阅</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="保密等级" align="center" prop="secrecyLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.secret_level" :value="scope.row.secrecyLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="档案盒号" align="center" prop="boxNumber" />
      <el-table-column label="纸质档案存放地址" align="center" prop="location" />
      <template>
      <el-table-column
        label="文件"
        align="center"
        prop="filePath"
        min-width="300px"
        show-overflow-tooltip
        fixed="right"
      >
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.filePath == null || scope.row.filePath === ''">无文件</el-tag>
          <div v-if="scope.row.filePath != null && scope.row.filePath !== ''">
            <li
              :key="path"
              class="el-upload-list__item ele-upload-list__item-content"
              v-for="path in getFilePath(scope.row.filePath)"
            >
              <el-link :href="path" :underline="false" target="_blank">
                <span class="el-icon-document">
                  {{
                  getFileName(path)
                  }}
                </span>
              </el-link>
            </li>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="imagePath" width="100" fixed="right">
        <template slot-scope="scope">
          <el-tag type="info" v-if="scope.row.imagePath == null || scope.row.imagePath === ''">无图片</el-tag>
          <image-preview
            v-if="scope.row.imagePath != null && scope.row.imagePath !== ''"
            :src="scope.row.imagePath"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      </template>
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"   fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:DArchive:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:DArchive:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加档案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="档案名称" prop="archiveName">
          <el-input v-model="form.archiveName" placeholder="请输入档案名称" />
        </el-form-item>
        <el-form-item label="档案类型" prop="archiveTypeId">
          <el-radio-group v-model="form.archiveTypeId">
            <el-radio
              v-for="dict in dict.type.archive_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="企业名称" prop="enterpriseName">
          <el-select
            v-model="form.enterpriseId"
            filterable
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="item in enterOptions"
              :key="item.id"
              :label="item.enterprisename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="企业id(通过名称找到)" prop="enterpriseId">
          <el-txet v-model="form.enterpriseId" placeholder="请输入企业id" />
        </el-form-item> -->

       <!-- <el-form-item label="部门id" prop="departmentId">
          <el-txet v-model="form.departmentId" placeholder="请输入部门id" />
        </el-form-item> -->
         <el-form-item label="部门名称" prop="departmentName">
          <el-input v-model="form.departmentName" placeholder="请输入部门名称" ></el-input>
        </el-form-item>

        <el-form-item label="档案内容简述" prop="archiveContent">
          <el-input v-model="form.archiveContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <template>
          <el-form-item label="图片上传" prop="imagePath">
            <image-upload v-model="form.imagePath" :fileType="imgType" />
          </el-form-item>
          <el-form-item label="文件上传" prop="filePath">
            <file-upload v-model="form.filePath" />
          </el-form-item>
        </template>
        <el-form-item label="所属案卷名" prop="docName">
          <el-input v-model="form.docName" placeholder="请输入所属案卷名" />
        </el-form-item>
        <el-form-item label="所属案卷编号" prop="docNumber">
          <el-input v-model="form.docNumber" placeholder="请输入所属案卷编号" />
        </el-form-item>
        <el-form-item label="保管期限" prop="saveTime">
          <el-input v-model="form.saveTime" placeholder="请输入保管期限" />
        </el-form-item>
        <el-form-item label="是否可借阅" prop="borrowState">
          <el-input v-model="form.borrowState" placeholder="请输入是否可借阅，与保密等级有关，机密不可借阅，解密可以借阅" />
        </el-form-item>
        <el-form-item label="密级" prop="secrecyLevel">
              <el-select v-model="form.secrecyLevel" filterable clearable placeholder="请选择">
                <el-option
                  v-for="item in dict.type.secret_level"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
        <el-form-item label="档案盒号" prop="boxNumber">
          <el-input v-model="form.boxNumber" placeholder="请输入档案盒号" />
        </el-form-item>
        <el-form-item label="纸质档案存放地址" prop="location">
          <el-input v-model="form.location" placeholder="请输入纸质档案存放地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDArchive, getDArchive, delDArchive, addDArchive, updateDArchive } from "@/api/system/DArchive";
import { listDEnterprise, getDEnterprise, delDEnterprise, addDEnterprise, updateDEnterprise } from "@/api/system/DEnterprise";

export default {
  name: "DArchive",
  dicts: ['archive_type', 'secret_level'],
  data() {
    return {
      // 企业
      enterOptions: [],
      //上传图片类型
      imgType: ["png", "jpg", "jpeg"],
      // 项目根路径
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 档案管理表格数据
      DArchiveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        archiveName: null,
        archiveType: null,
        enterpriseName: null,
        enterpriseId: null,
        departmentId: null,
        departmentName: null,
        userId: null,
        userName: null,
        archiveContent: null,
        archiveAddress: null,
        docName: null,
        docNumber: null,
        saveTime: null,
        borrowState: null,
        secrecyLevel: null,
        boxNumber: null,
        location: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        archiveName: [
          { required: true, message: "档案名称不能为空", trigger: "blur" }
        ],
        archiveTypeId: [
          { required: true, message: "档案类型id不能为空", trigger: "change" }
        ],
        enterpriseId: [
          { required: true, message: "企业id不能为空", trigger: "blur" }
        ],
        departmentId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getenterList();
  },
  methods: {
    /** 获取企业下拉菜单 */
    getenterList() {
      listDEnterprise({ pageNum: 1, pageSize: 100 }).then(
        (response) => {
          this.enterOptions = response.rows;
        }
      );
    },
    /** 查询档案管理列表 */
    getList() {
      this.loading = true;
      listDArchive(this.queryParams).then(response => {
        this.DArchiveList = response.rows;
        console.log("DArchiveList:", this.DArchiveList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        archiveName: null,
        archiveType: null,
        enterpriseName: null,
        enterpriseId: null,
        departmentId: null,
        departmentName: null,
        userId: null,
        userName: null,
        archiveContent: null,
        archiveAddress: null,
        docName: null,
        docNumber: null,
        saveTime: null,
        borrowState: null,
        secrecyLevel: null,
        boxNumber: null,
        location: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "档案添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this
      this.reset();
      const id = row.id || this.ids
      getDArchive(id).then(response => {
        that.form = response.data;
        console.log("form", this.form)
        that.open = true;
        that.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDArchive(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDArchive(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除档案管理编号为"' + ids + '"的数据项？').then(function() {
        return delDArchive(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/DArchive/export', {
        ...this.queryParams
      }, `DArchive_${new Date().getTime()}.xlsx`)
    },

    // 文件路径处理
    getFilePath(filePaths) {
      var arrayPath = filePaths.split(",");
      for (let i in arrayPath) {
        arrayPath[i] = this.baseUrl + arrayPath[i];
      }
      return arrayPath;
    },
    // 获取文件名称
    getFileName(name) {
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1).toLowerCase();
      } else {
        return "";
      }
    },

  }
};
</script>
